home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MultiSession 1.04 Source / Core 27⁄June⁄1993 / CDiskCache.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-22  |  4.7 KB  |  154 lines  |  [TEXT/KAHL]

  1. /* CDiskCache.h */
  2.  
  3. #pragma once
  4.  
  5. #include "EventLoop.h"
  6. #include "Memory.h"
  7. #include "File.h"
  8.  
  9. class CSack;
  10.  
  11. /* options for IDiskCache */
  12. #define CreatePermanent (-4)
  13. #define CreateTemporary (-3)
  14. #define OpenExisting (-2)
  15. #define CopyToTemporaryAndOpen (-1)
  16.  
  17. /* errors returned from IDiskCache */
  18. #define DiskError (-1)
  19. #define FileCorrupted (-2)
  20.  
  21. /* options for FlushAll */
  22. #define DoFlushHeldBlocks (True)
  23. #define DontFlushHeldBlocks (False)
  24.  
  25. typedef ulong IndexType;  /* typename for indices */
  26.  
  27. typedef Handle* Trandle;
  28.  
  29. /* The file's structure is: */
  30. /* ulong: Total size of file */
  31. /* ulong: "Special" Index */
  32. /* ulong: Number of blocks in file (allocated and unallocated) */
  33. /* (as many as needed:) */
  34. /*  ulong: Size of block */
  35. /*  ulong: Index of block (when read into memory), 0xffffffff = not allocated */
  36. /*  [size of block] bytes containing data for the object */
  37. #define UnallocatedIndex (0xffffffff)
  38. typedef struct
  39.     {
  40.         ulong        Size; /* total number of bytes in block (including header) */
  41.         ulong        Index;  /* index of block (or 0xffffffff if not allocated) */
  42.         ushort    Correction;  /* number of wasted bytes */
  43.     } FileBlockNode;
  44.  
  45. #define TOTALSIZEOFFSET (0)
  46. #define SPECIALINDEXOFFSET (TOTALSIZEOFFSET + sizeof(long))
  47. #define NUMBLOCKSOFFSET (SPECIALINDEXOFFSET + sizeof(long))
  48. #define MAXINDEXOFFSET (NUMBLOCKSOFFSET + sizeof(long))
  49. #define DATASTARTOFFSET (MAXINDEXOFFSET + sizeof(long))
  50.  
  51. /* this is one record from the array making up one hash slice */
  52. typedef struct
  53.     {
  54.         Handle        HandleToData; /* NIL == not loaded */
  55.             /* if UsedCell == False, then HandleToData is a pointer to next free hash entry */
  56.         long            FileOffset; /* offset into file of block's header */
  57.             /* if UsedCell == False, then FileOffset is the index of this hash entry */
  58.         MyBoolean    UsedCell : 1; /* false = available for allocation, true = used */
  59.         MyBoolean    Changed : 1; /* true = write data back to disk */
  60.         MyBoolean    NoPurge : 1; /* true = can't be unloaded */
  61.         short            Unused; /* rounds it out to 12 bytes, to cause long alignment */
  62.     } HashSliceEntry;
  63.  
  64. /* this is the number of cells per hash table slice (must be integral power of 2) */
  65. #define HASHSLICESIZE 128
  66. #define HASHSLICEMASK (HASHSLICESIZE-1)
  67.  
  68.  
  69. /* this is one slice from the hash table */
  70. typedef struct
  71.     {
  72.         HashSliceEntry*    SlicePtr; /* actually:  HashSliceEntry SlicePtr[HASHSLICESIZE] */
  73.     } HashTableEntry;
  74.  
  75.  
  76. typedef struct
  77.     {
  78.         long        FileOffset;
  79.         long        Size;
  80.     } FreeBlockEntry;
  81.  
  82.  
  83. struct    CDiskCache    :    CIdle
  84.     {
  85.      protected:
  86.         static IndexType    LastIndexPurged;
  87.         static MyBoolean    GrowZoneInstalled;
  88.      public:
  89.         CDiskCache*                NextCache;
  90.      protected:
  91.         CDiskCache*                PreviousCache;
  92.         HashTableEntry**    HashTableHandle;
  93.         long                            TotalSize;
  94.         long                            FreeSpace;
  95.         long                            NumBlocks;
  96.         short                            FileReference;
  97.         FSSpec                        DiskFile;
  98.         MyBoolean                    DeleteWhenFinished;
  99.         long                            MaxIndex;
  100.         MyBoolean                    FileModified;
  101.         HashSliceEntry*        FreeHashEntry;
  102.         CSack*                        FreeBlockList;
  103.         EXECUTE(MyBoolean DiskCacheInitFlag;)
  104.  
  105.      public:
  106.         /* */                    CDiskCache();
  107.         /* */                    ~CDiskCache();
  108.         short                    IDiskCache(FSSpec* FileInfo, short Mode, OSType FileType);
  109.         MyBoolean            SaveAs(FSSpec* WhereToSave, ulong Creator, ulong FileType);
  110.  
  111.         MyBoolean            DiskNew(long BlockSize, IndexType* IndexOut);
  112.         void                    DiskDispose(IndexType Index);
  113.         MyBoolean            DiskLoad(IndexType Index);
  114.         MyBoolean            DiskHold(IndexType Index);
  115.         void                    DiskUnhold(IndexType Index);
  116.         void                    DiskUnload(IndexType Index);
  117.         MyBoolean            DiskResize(long NewBlockSize, IndexType Index);
  118.         void                    DiskChanged(IndexType Index);
  119.         void                    DiskSave(IndexType Index);
  120.  
  121.         MyBoolean            AttachHandle(Handle ObjToAttach, IndexType* IndexOut);
  122.         Handle                Detach(IndexType Index);
  123.  
  124.         Trandle                Index2Trandle(IndexType Index);
  125.         IndexType            Trandle2Index(Trandle BlockRef);
  126.         Trandle                Handle2Trandle(Handle BlockHandle);
  127.         IndexType            Handle2Index(Handle BlockHandle);
  128.  
  129.         void                    SetSpecialIndex(IndexType Index);
  130.         IndexType            GetSpecialIndex(void);
  131.  
  132.         void                    CompactFile(void);
  133.         void                    FlushAll(MyBoolean FlushHeldBlocks);
  134.         void                    SaveAll(void);
  135.  
  136.      private:
  137.         IndexType            FindFreeHashEntry(void);
  138.         MyBoolean            ValidateHashEntry(IndexType Index);
  139.         void                    ReleaseHashEntry(IndexType Index);
  140.         MyBoolean            AllocateDiskBlock(long NumBytes, long* IndexToBlockStart,
  141.                                         IndexType Index);
  142.         void                    ReleaseDiskBlock(long IndexToBlockStart);
  143.         void                    SaveBlock(HashSliceEntry* BlockInfo);
  144.         long                    SwapBlockOut(HashSliceEntry* BlockInfo);
  145.         MyBoolean            SwapBlockIn(HashSliceEntry* BlockInfo);
  146.         void                    FlushVitalStats(void);
  147.         void                    DoIdle(long TimeSinceLastEvent);
  148.         long                    PurgeSome(long SizeNeeded, IndexType* StartingIndex);
  149.         MyBoolean            InitAll(void);
  150.         static long        MyGrowZone(ulong SizeNeeded);
  151.     };
  152.  
  153. void        SystemErrorFlushAllDiskCaches(void);
  154.